package com.chinanet.service.faultanticipate.flowmodel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * Created by moon on 16/9/27.
 */
public class Branch {
    public JSONArray list;
    public Branch(String json){
        list = JSON.parseArray(json);
    }

    //是否满足
    public boolean isSatisfy(String name,Map<String,Object> value){
        for(int i=0;i<list.size();i++){
            JSONObject obj = (JSONObject) list.get(i);
            for (Map.Entry<String, Object> entry : obj.entrySet()){
                if(entry.getKey().equals(name)){
                    return istrue(entry.getValue(),value);
                }
            }
        }
        return false;
    }


    //是否是默认条件
    public boolean isDefault(String name){
        for(int i=0;i<list.size();i++){
            JSONObject obj = (JSONObject) list.get(i);
            for (Map.Entry<String, Object> entry : obj.entrySet()) {
                if(entry.getKey().equals(name)&&entry.getValue().equals("default")){
                    return true;
                }
            }
        }
        return false;
    }

    //判断条件是否成立
    private static boolean istrue(Object data,Map<String,Object> value){
        if(data.equals("default")){
            return false;
        }else {
            //这里翻译 表达式
            Translate_data t_data=new Translate_data();
            String result=t_data.init((String) data,value);
            System.out.println(result);
            if(result.equals("true")){
                return true;
            }else{
                return  false;
            }
        }
    }
}
